package cn.itcast.demo;

import cn.itcast.domain.User;
import cn.itcast.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class UserDemo {
    private SqlSession sqlSession = null;
    private UserMapper mapper = null;
    @Before
    public  void  before() throws Exception{
        //读取mybatis核心配置文件
        InputStream is = Resources.getResourceAsStream("SqlConfig.xml");
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
        sqlSession = sessionFactory.openSession();
         mapper = sqlSession.getMapper(UserMapper.class);
    }

    @After
    public void after(){
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void findAll(){
        List<User> users = mapper.findAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
    @Test
    public void insert(){
        User u = new User();
        u.setUsername("美佳");
        u.setAddress("爱情公寓");
        mapper.insert(u);
    }
    @Test
    public void delete(){
        mapper.delete(30);
    }
    @Test
    public void update(){
        User u = new User();
        u.setId(35);
        u.setUsername("张益达");
        mapper.update(u);
    }
    @Test
    public void findUsernameAndPwd(){
        User u = mapper.findUsernameAndPwd("张三", "123");
        System.out.println(u);
    }
    @Test
    public void findUser(){
        List<User> users = mapper.findUser();
        System.out.println(users);
    }
}

















